{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "# Here is where we import all the things we need\n",
    "# This example is based on Abhishek's Machine Learning Demystified\n",
    "# https://github.com/shekit/machine-learning-demystified/\n",
    "\n",
    "import keras\n",
    "from keras.models import load_model\n",
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# helper function to sample an index from a probability array\n",
    "# This comes directly from \n",
    "# https://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py\n",
    "def sample(preds, temperature=1.0):\n",
    "    preds = np.asarray(preds).astype('float64')\n",
    "    preds = np.log(preds) / temperature\n",
    "    exp_preds = np.exp(preds)\n",
    "    preds = exp_preds / np.sum(exp_preds)\n",
    "    probas = np.random.multinomial(1, preds, 1)\n",
    "    return np.argmax(probas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# We have to build the same dictionaries here from the source text\n",
    "# We could instead save and load these instead?\n",
    "maxlen = 40\n",
    "# read the file and convert to lowercase (must match how we did it during training)\n",
    "text = open('data/hamlet.txt').read().lower()\n",
    "# Get a list of all the unique characters\n",
    "chars = sorted(list(set(text)))\n",
    "# Two dictionaries\n",
    "# Lookup a character by index\n",
    "# Lookup an index by character\n",
    "char_indices = dict((c,i) for i, c in enumerate(chars))\n",
    "indices_char = dict((i,c) for i,c in enumerate(chars))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# Load the model\n",
    "model = load_model('model.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'d then i precepts gave her,\\nthat she should this is hamlet,\\nthere not say you think at the'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# OK, let's generate some new text\n",
    "# Pick a random part of the source text\n",
    "start_index = random.randint(0, len(text) - maxlen - 1)\n",
    "# Diversity or temperature: higher is wackier\n",
    "diversity = 0.5\n",
    "# Text we will generate\n",
    "generated = ''\n",
    "# Text to feed into the LSTM\n",
    "sentence = text[start_index: start_index + maxlen]\n",
    "# Generated text will include that\n",
    "generated += sentence\n",
    "\n",
    "# Generate 50 new characters\n",
    "for i in range(50):\n",
    "    # First vectorize the text we are feeding in\n",
    "    x = np.zeros((1, maxlen, len(chars)))\n",
    "    for t, char in enumerate(sentence):\n",
    "        x[0, t, char_indices[char]] = 1.\n",
    "    # Then get a probability map of possible predictions of the next char\n",
    "    preds = model.predict(x, verbose=0)[0]\n",
    "    # Pick one\n",
    "    next_index = sample(preds, diversity)\n",
    "    # What character is it?\n",
    "    next_char = indices_char[next_index]\n",
    "    # Add it\n",
    "    generated += next_char\n",
    "    # Now feed in the previous sentence minus the first char plus the next char\n",
    "    sentence = sentence[1:] + next_char\n",
    "generated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
